package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.strategy.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.strategy.*;
import com.miplus.ccrm.core.utils.SpringContextUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-3.任务策略管理服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/cpg")
public class StraController {
    @Resource
    private CpCalllistService cpCalllistService;
    @Resource
    private CpStrategyService cpStrategyService;
    @Resource
    private CpCalllistTriggerService cpCalllistTriggerService;
    @Resource
    private CpCalllistTriggerQuerylinkService cpCalllistTriggerQuerylinkService;

    @Resource
    private CfgActivityService cfgActivityService;
    @Resource
    private CfgActivityQuerylinkService cfgActivityQuerylinkService;
    @Resource
    private CfgActivityQuerydatacolService cfgActivityQuerydatacolService;
    @Resource
    private CfgAssignsetService cfgAssignsetService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private CpScriptTempService cpScriptTempService;
    @Resource
    private CampaignService campaignService;
    @Resource
    private FiCpdoTraceService traceService;

    /**
     * 取案件任务LIST
     */
    @GetMapping("/campaignList")
    public R<PageData<CpListResVo>> campaignList(@Validated CpListReqVo cpListReqVo){
        return R.SuccessPage(cpCalllistService.campaignList(cpListReqVo));
    }

    /**
     * 添加案件任务
     */
    @PostMapping("/campaignInsert")
    public R<CmdResVo> campaignInsert(@RequestBody @Validated CpListEditVo cpListEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        //String usrid= "admin";
        return R.Success(cpCalllistService.campaignInsert(usrid,cpListEditVo));
    }

    /**
     * 修改案件任务
     */
    @PostMapping("/campaignUpdate")
    public R<CmdResVo> campaignUpdate(@RequestBody @Validated CpListEditVo cpListEditVo) {
        String usrid= jCommonData.getCurrUsrID();
          //String usrid= "admin";
        return R.Success(cpCalllistService.campaignUpdate(usrid,cpListEditVo));
    }

    /**
     * 删除案件任务
     */
    @PostMapping("/campaignDelete")
    public R<CmdResVo> campaignDelete(@RequestBody @Validated CpListIDVo cpListIDVo) {
        return R.Success(cpCalllistService.campaignDelete(cpListIDVo.getClid()));
    }

    /**
     * 取案件任务ID.LIST
     */
    @GetMapping("/campaignSelectList")
    public R<List<LabelResVo>> campaignSelectList(Integer status,Integer channel){

        return R.Success(cpCalllistService.campaignSelectList(status,channel));
    }

    /**
     * 取外呼任务ID.LIST
     */
    @GetMapping("/dialSelectList")
    public R<List<LabelResVo>> dialSelectList(Integer status){
        return R.Success(cpCalllistService.dialSelectList(status));
    }

    /**
     * 取质检任务ID.LIST
     * 不加@RequestParam参数，表示非必传
     */
    @GetMapping("/qmSelectList")
    public R<List<LabelResVo>> qmSelectList(Integer status){
        return R.Success(cpCalllistService.qmSelectList(status));
    }

    /**
     * 取任务策略LIST
     */
    @GetMapping("/strategyList")
    public R<PageData<StrategyResVo>> strategyList(@Validated StrategyReqVo strategyReqVo){
        return R.SuccessPage(cpStrategyService.strategyList(strategyReqVo));
    }

    /**
     * 取任务策略ID.LIST
     */
    @GetMapping("/contactResults")
    public R<List<LabelResVo>> contactResults(@RequestParam @NotNull String clid){
        return R.Success(cpStrategyService.contactResults(clid));
    }

    /**
     * 添加任务策略
     */
    @PostMapping("/strategyInsert")
    public R<CmdResVo> strategyInsert(@RequestBody @Validated StrategyEditVo strategyEditVo) {
        return R.Success(cpStrategyService.strategyInsert(strategyEditVo));
    }

    /**
     * 修改任务策略
     */
    @PostMapping("/strategyUpdate")
    public R<CmdResVo> cstrategyUpdate(@RequestBody @Validated StrategyEditVo strategyEditVo) {
        return R.Success(cpStrategyService.strategyUpdate(strategyEditVo));
    }

    /**
     * 删除任务策略
     */
    @PostMapping("/strategyDelete")
    public R<CmdResVo> strategyDelete(@RequestBody @Validated StrategyIDVo strategyIDVo) {
        return R.Success(cpStrategyService.strategyDelete(strategyIDVo.getStrategyid()));
    }

    /**
     * 取任务触发器详细信息LIST
     */
    @GetMapping("/triggerList")
    public R<PageData<TriggerResVo>> triggerList(@Validated TriggerReqVo triggerReqVo){
        return R.SuccessPage(cpCalllistTriggerService.triggerList(triggerReqVo));
    }

    /**
     * 取任务触发器Value LIST
     */
    @GetMapping("/getTriggerList")
    public R<List<LabelResVo>> getTriggerList(){
        return R.Success(cpCalllistTriggerService.getTriggerList());
    }

    /**
     * 添加任务触发器
     */
    @PostMapping("/triggerInsert")
    public R<CmdResVo> triggerInsert(@RequestBody @Validated TriggerEditVo triggerEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cpCalllistTriggerService.triggerInsert(usrid,triggerEditVo));
    }

    /**
     * 修改任务触发器
     */
    @PostMapping("/triggerUpdate")
    public R<CmdResVo> triggerUpdate(@RequestBody @Validated TriggerEditVo triggerEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cpCalllistTriggerService.triggerUpdate(usrid,triggerEditVo));
    }

    /**
     * 删除任务触发器
     */
    @PostMapping("/triggerDelete")
    public R<CmdResVo> triggerDelete(@RequestBody @Validated TriggerIDVo triggerIDVo) {
        return R.Success(cpCalllistTriggerService.triggerDelete(triggerIDVo.getTriggerid()));
    }

    /**
     * 取筛选目标LIST
     */
    @GetMapping("/triggerColList")
    public R<List<LabelResVo>> triggerColList(){
        return R.Success(cfgActivityQuerydatacolService.triggerColList());
    }

    /**
     * 取触发器对应规则LIST
     */
    @GetMapping("/triggerRuleList")
    public R<PageData<TriglinkResVo>> queryRuleListTrigger(@Validated TriglinkReqVo triglinkReqVo){
        return R.SuccessPage(cpCalllistTriggerQuerylinkService.queryRuleListTrigger(triglinkReqVo));
    }

    /**
     * 添加任务触发器规则
     */
    @PostMapping("/triggerRuleInsert")
    public R<CmdResVo> insertRuleListTrigger(@RequestBody @Validated TriglinkEditVo triglinkEditVo) {
        return R.Success(cpCalllistTriggerQuerylinkService.insertQueryRuleTrigger(triglinkEditVo));
    }

    /**
     * 修改任务触发器规则
     */
    @PostMapping("/triggerRuleUpdate")
    public R<CmdResVo> updateQueryRuleTrigger(@RequestBody @Validated TriglinkEditVo triglinkEditVo) {
        return R.Success(cpCalllistTriggerQuerylinkService.updateQueryRuleTrigger(triglinkEditVo));
    }

    /**
     * 删除任务触发器规则
     */
    @PostMapping("/triggerRuleDelete")
    public R<CmdResVo> deleteQueryRuleTrigger(@RequestBody @Validated TriglinkIDVo triglinkIDVo) {
        return R.Success(cpCalllistTriggerQuerylinkService.deleteQueryRuleTrigger(triglinkIDVo.getQueryruleid()));
    }

    /**
     * 取自动分案模板LIST
     */
    @GetMapping("/cfgActivityList")
    public R<PageData<ActivityResVo>> cfgActivityList(@Validated ActivityReqVo activityReqVo){
        return R.SuccessPage(cfgActivityService.cfgActivityList(activityReqVo));
    }


    /**
     * 添加自动分案模板
     */
    @PostMapping("/cfgActivityInsert")
    public R<CmdResVo> cfgActivityInsert(@RequestBody @Validated ActivityEditVo activityEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgActivityService.cfgActivityInsert(usrid,activityEditVo));
    }

    /**
     * 修改自动分案模板
     */
    @PostMapping("/cfgActivityUpdate")
    public R<CmdResVo> cfgActivityUpdate(@RequestBody @Validated ActivityEditVo activityEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgActivityService.cfgActivityUpdate(usrid,activityEditVo));
    }

    /**
     * 删除自动分案模板
     */
    @PostMapping("/cfgActivityDelete")
    public R<CmdResVo> cfgActivityDelete(@RequestBody @Validated ActivityIDVo activityIDVo) {
        return R.Success(cfgActivityService.cfgActivityDelete(activityIDVo.getActivityid()));
    }

    /**
     * 自动分案模板上移
     */
    @PostMapping("/upCfgActivity")
    public R<CmdResVo> upCfgActivity(@RequestBody @Validated ActivityIDVo activityIDVo) {
        return R.Success(cfgActivityService.upCfgActivity(activityIDVo.getActivityid()));
    }

    /**
     * 自动分案模板下移
     */
    @PostMapping("/downCfgActivity")
    public R<CmdResVo> downCfgActivity(@RequestBody @Validated ActivityIDVo activityIDVo) {
        return R.Success(cfgActivityService.downCfgActivity(activityIDVo.getActivityid()));
    }

    /**
     * 自动分案模板规则LIST
     */
    @GetMapping("/activityRuleList")
    public R<PageData<ActRuleResVo>> activityRuleList(@Validated ActRuleReqVo actRuleReqVo){
        return R.SuccessPage(cfgActivityQuerylinkService.activityRuleList(actRuleReqVo));
    }

    /**
     * 添加自动分案模板规则
     */
    @PostMapping("/activityRuleInsert")
    public R<CmdResVo> activityRuleInsert(@RequestBody @Validated ActRuleEditVo actRuleEditVo) {
        return R.Success(cfgActivityQuerylinkService.activityRuleInsert(actRuleEditVo));
    }

    /**
     * 修改自动分案模板规则
     */
    @PostMapping("/activityRuleUpdate")
    public R<CmdResVo> activityRuleUpdate(@RequestBody @Validated ActRuleEditVo actRuleEditVo) {
        return R.Success(cfgActivityQuerylinkService.activityRuleUpdate(actRuleEditVo));
    }

    /**
     * 删除自动分案模板规则
     */
    @PostMapping("/activityRuleDelete")
    public R<CmdResVo> activityRuleDelete(@RequestBody @Validated ActRuleIDVo actRuleIDVo) {
        return R.Success(cfgActivityQuerylinkService.activityRuleDelete(actRuleIDVo.getQueryruleid()));
    }

    /**
     * 修改自动分案模板规则状态
     */
    @PostMapping("/activityRuleUpStatus")
    public R<CmdResVo> activityRuleUpStatus(@RequestBody @Validated ActRuleStatusVo statusVo) {
        return R.Success(cfgActivityQuerylinkService.activityRuleUpStatus(statusVo));
    }

    /**
     * 自动分案模板关联组或机构信息LIST
     */
    @GetMapping("/activityGroupList")
    public R<PageData<ActOGrpLinkResVo>> activityGroupList(@Validated ActOGrpLinkReqVo actOGrpLinkReqVo){
        return R.SuccessPage(cfgActivityService.activityOutGrpList(actOGrpLinkReqVo));
    }

    /**
     * 取自动分案模板对应执行日List
     */
    @GetMapping("/getActivityExecDayList")
    public R<List<ActExecDayVo>> getActivityExecDayList(@Validated Integer activityId){
        return R.Success(cfgActivityService.getActivityExecDay(activityId));
    }

    /**
     * 自动派工模板规则LIST
     */
    @GetMapping("/assignSetList")
    public R<PageData<AssSetResVo>> assignSetList(@Validated AssSetReqVo assSetReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.SuccessPage(cfgAssignsetService.assignSetList(usrid,assSetReqVo));
    }

    /**
     * 添加自动派工模板规则
     */
    @PostMapping("/assignSetInsert")
    public R<CmdResVo> assignSetInsert(@RequestBody @Validated AssSetEditVo assSetEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgAssignsetService.assignSetInsert(usrid,assSetEditVo));
    }

    /**
     * 修改自动派工模板规则
     */
    @PostMapping("/assignSetUpdate")
    public R<CmdResVo> assignSetUpdate(@RequestBody @Validated AssSetEditVo assSetEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgAssignsetService.assignSetUpdate(usrid,assSetEditVo));
    }

    /**
     * 删除自动派工模板规则
     */
    @PostMapping("/assignSetDelete")
    public R<CmdResVo> assignSetDelete(@RequestBody @Validated AssSetDelVo assSetDelVo) {
        return R.Success(cfgAssignsetService.assignSetDelete(assSetDelVo.getGrpid()));
    }

    /**
     * 取自动派工催员信息GRID
     */
    @GetMapping("/assignRatioGrid")
    public R<List<AssRatioResVo>> assignRatioGrid(@RequestParam @NotNull String grpid){
        return R.Success(cfgAssignsetService.assignRatioList(grpid));
    }

    /**
     * 取通道对应的模板LIST
     */
    @GetMapping("/getTemplateList")
    public R<List<LabelResVo>> getTemplateList(@RequestParam @NotNull Integer channelid){
        return R.Success(cpCalllistService.getTemplateList(channelid));
    }

    /**
     * 取画布模板LIST
     */
    @GetMapping("/scriptTempList")
    public R<PageData<ScriptResVo>> scriptTempList(@Validated ScriptReqVo scriptReqVo){
        return R.SuccessPage(cpScriptTempService.scriptList(scriptReqVo));
    }

    /**
     * 取画布模板详细信息
     */
    @GetMapping("/getSciptDetail")
    public R<ScriptResVo> getSciptDetail(@RequestParam @NotNull Integer tempid){
        return R.Success(cpScriptTempService.getSciptDetail(tempid));
    }

    /**
     * 添加画布模板
     */
    @PostMapping("/scriptTempInsert")
    public R<CmdResVo> scriptTempInsert(@RequestBody @Validated ScriptEditVo scriptEditVo) {
        //String usrid= jCommonData.getCurrUsrID();
        String usrid= "feixiang";
        return R.Success(cpScriptTempService.scriptInsert(usrid,scriptEditVo));
    }

    /**
     * 修改画布模板
     */
    @PostMapping("/scriptTempUpdate")
    public R<CmdResVo> scriptTempUpdate(@RequestBody @Validated ScriptEditVo scriptEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cpScriptTempService.scriptUpdate(usrid,scriptEditVo));
    }

    /**
     * 删除画布模板
     */
    @PostMapping("/scriptTempDelete")
    public R<CmdResVo> scriptTempDelete(@RequestBody @Validated ScriptDelVo scriptDelVo) {
        return R.Success(cpScriptTempService.scriptDelete(scriptDelVo.getScriptid()));
    }

    /**
     * 生成画布机器人脚本
     */
    @PostMapping("/buildRobotScript")
    public R<CmdResVo> buildRobotScript(@RequestBody @Validated ScriptNodeVo scriptNodeVo) {
        return R.Success(cpScriptTempService.buildRobotScript(scriptNodeVo.getScripttxt()));
    }

    /**
     * 取筛选器变量LIST
     */
    @GetMapping("/cfgQueryColList")
    public R<PageData<QuyColResVo>> cfgQueryColList(@Validated QuyColReqVo colResVo){
        return R.SuccessPage(cfgActivityQuerydatacolService.cfgQueryColList(colResVo));
    }

    /**
     * 更新筛选器变量
     */
    @PostMapping("/cfgQueryColUpdate")
    public R<CmdResVo> cfgQueryColUpdate(@RequestBody @Validated QuyColEditVo colEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgActivityQuerydatacolService.cfgQueryColUpdate(usrid,colEditVo));
    }

    /**
     * 插入筛选器变量
     */
    @PostMapping("/cfgQueryColInsert")
    public R<CmdResVo> cfgQueryColInsert(@RequestBody @Validated QuyColEditVo colEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgActivityQuerydatacolService.cfgQueryColInsert(usrid,colEditVo));
    }

    /**
     * 删除筛选器变量
     */
    @PostMapping("/cfgQueryColDelete")
    public R<CmdResVo> cfgQueryColDelete(@RequestBody @Validated QuyColDelVo colDelVo) {
        return R.Success(cfgActivityQuerydatacolService.cfgQueryColDelete(colDelVo.getId()));
    }

    /**
     * 更新筛选器变量状态
     */
    @PostMapping("/cfgQueryColUpStatus")
    public R<CmdResVo> cfgQueryColUpStatus(@RequestBody @Validated QuyColStatusVo statusVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cfgActivityQuerydatacolService.cfgQueryColUpStatus(usrid,statusVo));
    }

    /**
     * 任务执行操作
     */
    @PostMapping("/cpTaskDo")
    public R<CmdResVo> cpTaskDo(@RequestBody @Validated CpDoReqVo reqVo) {
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(campaignService.cpTaskDo(user, SpringContextUtil.getRequestHost(),reqVo));
    }

    /**
     * 取任务执行日志LIST
     */
    @GetMapping("/cpTraceList")
    public R<PageData<CpTraResVo>> cpTraceList(@Validated CpTraReqVo reqVo){
        return R.SuccessPage(traceService.traceList(reqVo));
    }
}
